package com.uyayo.cms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uyayo.cms.entity.CmsAd;
import com.uyayo.cms.entity.CmsChannel;
import com.uyayo.cms.mapper.CmsAdMapper;
import com.uyayo.cms.model.CmsAdCondition;
import com.uyayo.cms.service.CmsAdService;
import com.uyayo.framework.pagehelper.pojo.OrderBy;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.framework.pagehelper.util.OrderByUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Service("cmsAdService")
public class CmsAdServiceImpl implements CmsAdService {


	private final CmsAdMapper cmsAdMapper;

	@Autowired
	public CmsAdServiceImpl(CmsAdMapper cmsAdMapper) {
		this.cmsAdMapper = cmsAdMapper;
	}


	@Override
	public CmsAd getAdById(Long id) {
		return cmsAdMapper.selectByPrimaryKey(id);
	}
	

	@Override
	public List<CmsAd> selectAd(CmsAdCondition cmsAdCondition) {
		Example example = new Example(CmsAd.class);
		Example.Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(cmsAdCondition.getCode())){
			criteria.andEqualTo("code", cmsAdCondition.getCode());
		}
		if(Objects.nonNull(cmsAdCondition.getStatus())){
			criteria.andEqualTo("status",cmsAdCondition.getStatus());
		}
		if(Objects.nonNull(cmsAdCondition.getAdCategoryId())){
			criteria.andEqualTo("adCategoryId",cmsAdCondition.getAdCategoryId());
		}
		return cmsAdMapper.selectByExample(example);
	}
	
	@Override
	public PageBean<CmsAd> selectAdByPaging(Integer pageCurrent, Integer pageSize, String sort, String order, CmsAdCondition cmsAdCondition) {

		List<CmsAd> userList;
		//分页处理
		List<OrderBy> orderBies = new ArrayList<>();
		orderBies.add(new OrderBy("createTime", "create_time"));
		orderBies.add(new OrderBy("updateTime", "update_time"));

		OrderByUtil  orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderBies, sort, order);
		PageHelper.startPage(pageCurrent, pageSize);
		
		Example example = new Example(CmsAd.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}
		Example.Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(cmsAdCondition.getName())){
			criteria.andLike("name","%"+ cmsAdCondition.getName()+"%");
		}
		if(!StringUtils.isEmpty(cmsAdCondition.getCode())){
			criteria.andEqualTo("code", cmsAdCondition.getCode());
		}
		if(cmsAdCondition.getBeginCreateTime()!=null || cmsAdCondition.getEndCreateTime()!=null){
			criteria.andBetween("createTime", cmsAdCondition.getBeginCreateTime(), cmsAdCondition.getEndCreateTime());
		}
		if(Objects.nonNull(cmsAdCondition.getAdCategoryId())){
			criteria.andEqualTo("adCategoryId",cmsAdCondition.getAdCategoryId());
		}
		if(Objects.nonNull(cmsAdCondition.getStatus())){
			criteria.andEqualTo("status",cmsAdCondition.getStatus());
		}
		userList = cmsAdMapper.selectByExample(example);

		//取记录总条数
		PageInfo<CmsAd> pageInfo = new PageInfo<>(userList);
		
		return new PageBean<>(pageInfo.getTotal(), pageCurrent, pageSize, userList);
	}
	

	@Override
	public int createCmsAd(CmsAd cmsAd) {
		return cmsAdMapper.insertSelective(cmsAd);
	}

	@Override
	public CmsAd selectOne(CmsAd cmsAd) {
		return cmsAdMapper.selectOne(cmsAd);
	}

	@Override
	public int updateAd(CmsAd cmsAd) {
		return cmsAdMapper.updateByPrimaryKeySelective(cmsAd);
	}

	@Override
	public int deleteAd(Integer id) {
		return cmsAdMapper.deleteByPrimaryKey(id);
	}
	
}
